<?

//Data loading
$title='LEMBAR INFORMASI PEKERJAAN (LIB)';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';
$jo_id = GetParam("jo_id","");
$lpj_id = GetParam("lpj_id","");
$TableName = "tbllpj";


$DEFINE_FONT=0;
$pdf=new PDFReport('P',"mm","Letter");
$pdf->AddPage();
//$Result = GridList ("", "", "");
//if ($Result) $pdf->GridTable($Result);
$pdf->Output();


class PDFReport extends PDF 
{

	function Header() {
  	  global $title,$perush,$page,$DEFINE_FONT,$ROOT_IMG_DIR, $jo_id, $lpj_id, $DBConnection,$DBGeneral,$TableName;
            
  	  if ($DEFINE_FONT==0) {
  		 $this->AddFont('arialn','','arialn.php');
  		 $DEFINE_FONT=1;
  	  }
      
      //Fetch Data Job Order
      $SQLJO = "SELECT a.*,b.*,c.*,d.*,e.nama as operation_staff_name FROM ".$TableName." as a
                  JOIN tbldebtur as b on a.debtur = b.CUCODE 
                  LEFT JOIN ".$DBGeneral.".tblstaff as c on a.staff_id = c.staff_id 
                  JOIN tblproject as d ON a.project_id = d.project_id 
				  LEFT JOIN ".$DBGeneral.".tblstaff as e on a.operation_staff_id = e.staff_id
                  WHERE a.lpj_id ='".$lpj_id."' ORDER BY a.lpj_id ASC";
      
      $ResultJO = $DBConnection->dbc->get_results($SQLJO,ARRAY_A); 
      
           
      //Get the JO Date
      list($year1,$month1,$day1) = explode("-",$ResultJO[0]['jo_date']);
      $jo_date  = "$day1/$month1/$year1";
      
      //Get the Project Start Date
      list($year2,$month2,$day2) = explode("-",$ResultJO[0]['start_date']);
      $proj_start  = "$day2/$month2/$year2";
      
      //Get the Project End Date
      list($year3,$month3,$day3) = explode("-",$ResultJO[0]['end_date']);
      $proj_end  = "$day3/$month3/$year3";
            
      $shipment = substr($ResultJO[0]['jo_code'],-3,3);            
    
             
      //End Fetch Data Job Order
           
	  $this->SetFont('arialn','',10);
	  
	  $this->Image($ROOT_IMG_DIR.'/flag_SI.jpg',12,11,34,20,'JPG');	  
	  $this->Cell(40,20,"",1,0,'C',0);
	  $this->SetFont('arialn','',18);
	  $this->Cell(110,20,"Biaya Operational Receipt",1,0,'C',0);
	  
	  $this->SetFont('arialn','',20);
	  $this->Cell(40,20,"MPC",1,0,'C',0);	  	  
	  $this->SetFont('arialn','',10);
	  
	  $this->Ln();	  
	  $this->Ln();	  
	  	  
	        
      //Text Form
      $this->Cell(50,5,"Customer",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,$ResultJO[0]['CUNAME'],0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"Nama Proyek",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,$ResultJO[0]['project_name'],0,0,'L',0);
      $this->Ln();      
      $this->Cell(50,5,"Date",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,$jo_date,0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"Perihal",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,"Biaya Operasional Receipt",0,0,'L',0);
      $this->Ln();      
      $this->Ln();
      
      $this->SetFont('arialn','',13);     
      $this->Cell(190,5,"B.Biaya Operasional Receipt",0,0,'L',0);
      $this->Ln();
      $this->Ln();
      
      $this->SetFont('arialn','',10);   
      
      //Grid
      $SQLGridItem = "SELECT * FROM tbllpjitem as a
				JOIN tbllpj b ON a.lpj_id = b.lpj_id
				JOIN tblactivity_setup c ON a.item_id = c.act_nr_id
                WHERE a.lpj_id ='".$lpj_id."' ORDER BY a.item_id";
	
	  $ResultItem  = $DBConnection->dbc->get_results($SQLGridItem,ARRAY_A);
      //Grid
      
      $totalCost = 0;
      
      foreach($ResultItem as $idx => $val)
      {
		  $this->Cell(145,5,$val['actname'],0,0,'L',0);
		  $this->Cell(15,5,"Rp.",0,0,'L',0);
		  $this->Cell(30,5,number_format($val['item_jumlah'],2),0,0,'R',0);
		  $this->Ln();
		  
		  $totalCost+= $val['item_jumlah'];
	  }
      
      $this->SetFont('arialn','',12);
      $this->Ln();
      $this->Cell(145,5,"TOTAL",0,0,'C',0);
      $this->Cell(15,5,"Rp.",0,0,'L',0);
      $this->Cell(30,5,number_format($totalCost,2),0,0,'R',0);
      $this->SetFont('arialn','',10);
      
      $this->Ln();
      $this->Ln();
      $this->Ln();
      
      
      $this->Cell(50,7,"BS Advance","LTB",0,'C',0);
      $this->Cell(30,7,"Advance Date","LTB",0,'C',0);
      $this->Cell(30,7,"Actual Cost","LTB",0,'C',0);
      $this->Cell(30,7,"Cost in Rupiah","LRTB",0,'C',0);
      $this->Ln();
      
      //BS
      $SQLGrid = "SELECT * FROM tbllpj_detail as a
	              WHERE lpj_id ='".$lpj_id."' ORDER BY a.lpj_detail_id";
	  	  	  
	  $Result = $DBConnection->dbc->get_results($SQLGrid,ARRAY_A);	  	  
	  //BS
	  
	  $totalCost = 0;
      
      if(!empty($Result))
      {
		  foreach($Result as $idx2 => $val2)
		  {
			  $this->Cell(50,7,$val2['adv_code'],"L",0,'C',0);
			  $this->Cell(30,7,$val2['adv_date'],"L",0,'C',0);
			  $this->Cell(30,7,number_format($val2['jumlah'],2),"L",0,'R',0);
			  $this->Cell(30,7,number_format($val2['rupiah'],2),"LR",0,'R',0);
			  $this->Ln();
			  
			  $totalCost+= $val['item_jumlah'];
		  }
		  
		  $this->Cell(50,7,"","LB",0,'C',0);
		  $this->Cell(30,7,"","LB",0,'C',0);
		  $this->Cell(30,7,"","LB",0,'R',0);
		  $this->Cell(30,7,"","LRB",0,'R',0);
      }
	  
	  $this->Ln();
	  $this->Ln();
	  $this->Ln();
	  $this->Ln();      
      $this->Cell(90,5,"Manager Operasi","LT",0,'L',0);      
      $this->Cell(90,5,"Supervisi 1","LTR",0,'L',0);
      $this->Ln();
	  $this->Cell(90,10," ","L",0,'L',0);
      $this->Cell(90,10," ","LR",0,'L',0);
      $this->Ln();	  
      $this->Cell(90,5,$ResultProf[0]['manager_operasi'],"LB",0,'C',0);
      $this->Cell(90,5,$ResultProf[0]['supervisi_1'],"LBR",0,'C',0);
      $this->Ln();
	  
	  
	  //END GRID
      
         
     
	}

	function GridTable($MyResult) {
	    //Column widths
	    $w=array(25,20,45,20,40,25,25);

	    //Data


	    foreach($MyResult as $row)
	    {
		    if ($row["dk"]=='D') $row["rupiah1"] = $row["rupiah"];
		    else $row["rupiah2"] = $row["rupiah"];
		    $totald = $totald + $row["rupiah1"];
		    $totalc = $totalc + $row["rupiah2"];
		    
	        if (($skada<>$row["skada"]) && (strlen($skada)>0)) {
		        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SUB TOTAL:",1,0,'R',0);
		        $this->Cell($w[5],5,number_format($subtotald+$SaldoDebet,2),1,0,'R',0);
				$this->Cell($w[6],5,number_format($subtotalc+$SaldoKredit,2),1,0,'R',0);
		        $this->Ln();
	        	$subtotald = 0;
	        	$subtotalc = 0;
	        	$this->AddPage();
        	}

		    //*********** SALDO AWAL *************
		    if (($skada<>$row["skada"])) {
			    $Saldo = $row["lastmonth"];
			    if ($Saldo>0) $SaldoDebet = $Saldo;
			    else $SaldoKredit = $Saldo;
	
			    $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SALDO AWAL (".$row["skada"]."):",'BTLR',0,'R');
			    $this->Cell($w[5],5,number_format($SaldoDebet,2),'LR',0,'R');
				$this->Cell($w[6],5,number_format($SaldoKredit,2),'LR',0,'R');
			    $this->Ln();
	    	}
			//*********** SALDO AWAL *************
        	
	        $subtotald = $subtotald + $row["rupiah1"];
	        $subtotalc = $subtotalc + $row["rupiah2"];

	        $this->Cell($w[0],5,$row["nobkt"],'LR');
	        $this->Cell($w[1],5,$row["trdate"],'LR',0,'C');
	        $this->Cell($w[2],5,$row["debnam"],'LR',0,'L');
	        $this->Cell($w[3],5,$row["skada"],'LR');
	        $this->Cell($w[4],5,$row["uraian"],'LR');
	        $this->Cell($w[5],5,number_format($row["rupiah1"],2),'LR',0,'R');
			$this->Cell($w[6],5,number_format($row["rupiah2"],2),'LR',0,'R');
	        $this->Ln();
	        $skada = $row["skada"];
	    }
		$this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SUB TOTAL:",1,0,'R',0);
		$this->Cell($w[5],5,number_format($subtotald+$SaldoDebet,2),1,0,'R',0);
		$this->Cell($w[6],5,number_format($subtotalc+$SaldoKredit,2),1,0,'R',0);
		$this->Ln();
/*
	    $this->Cell(130,5,"TOTAL:",1,0,'R',0);
	    $this->Cell(25,5,number_format($totald,2),1,0,'R',0);
		$this->Cell(25,5,number_format($totalc,2),1,0,'R',0);
	    $this->Ln();
*/
	    //Closure line
	}

	function Footer() {
	    //Closure line
	    //$this->Cell(180,0,'','T');
	}
	
	
	
	function convert_code($code,$digit)
	{
			
		$length = strlen($code);	
		$tot_str = $digit - $length;	
		$code_edit = "";
		
		for($i=0;$i < $tot_str; $i++)
		{
			$code_edit.= "0";
		}
		
		$code_edit.= $code;
		
		return $code_edit;
		
	}
	
	function get_counter_no($flag)
	{
		global $DBConnection;
		
		$SQL = "SELECT * FROM tblcounter_number WHERE flag = '".$flag."' " ;
		$result = $DBConnection->dbc->get_results($SQL,ARRAY_A);
		
		$no_position = $result[0]['no_position'];
		$no_position+= $result[0]['add'];
		
		$SQL2 = "UPDATE tblcounter_number SET no_position = '".$no_position."' WHERE flag = '".$flag."' " ;
		$DBConnection->dbc->query($SQL2,ARRAY_A);
		
		$counter_no = $this->convert_code($no_position,10);
		
		return $counter_no;
		
	}
	
	

}


?>
